package com.net.udp
{
	import com.net.handler.PacketHandler;
	
	import mx.core.Application;

	/**
	 * 具体的游戏逻辑
	 * 
	 */
	public class UDPServer
	{
		private static var _instance:UDPServer;
		
		private var _sceneGroupName:String; // 包括聊天和走动
		private var _teamGroupName:String;
		private var _battleGroupName:String; // 包括战斗走动和战斗动作
		private var _unionGroupName:String;
		private var _worldGroupName:String;
		private var _url:String;
		private var _packetHandler:PacketHandler = new PacketHandler;

		private var _groupManager:NetGroupManager = NetGroupManager.getInstance();
		
		public static function getInstance():UDPServer{
			if(!_instance){
				_instance = new UDPServer;
			}
			return _instance;
		}
		
		public function UDPServer()
		{
		}
		
		public function init(url:String, sceneGroupName:String, teamGroupName:String, battleGroupName:String, unionGroupName:String, worldGroupName:String):void{
			_url = url;
			_sceneGroupName = sceneGroupName;
			_teamGroupName = teamGroupName;
			_battleGroupName = battleGroupName;
			_unionGroupName = unionGroupName;
			_worldGroupName = worldGroupName;
			
			_groupManager.connect(_url, connectSuccess);
		}
		
		private function connectSuccess():void{
			_groupManager.addGroup(_sceneGroupName, onMessage);
			_groupManager.addGroup(_teamGroupName, onMessage);
			_groupManager.addGroup(_battleGroupName, onMessage);
			_groupManager.addGroup(_unionGroupName, onMessage);
			_groupManager.addGroup(_worldGroupName, onMessage);
		}
		
		public function set sceneGroupName(value:String):void
		{
			_groupManager.delGroup(_sceneGroupName);
			_sceneGroupName = value;
			_groupManager.addGroup(_sceneGroupName);
		}
		
		public function set teamGroupName(value:String):void
		{
			_groupManager.delGroup(_teamGroupName);
			_teamGroupName = value;
			_groupManager.addGroup(_teamGroupName);
		}
		
		public function set battleGroupName(value:String):void
		{
			_groupManager.delGroup(_battleGroupName);
			_battleGroupName = value;
			_groupManager.addGroup(_battleGroupName);
		}
		
		public function set unionGroupName(value:String):void
		{
			_groupManager.delGroup(_unionGroupName);
			_unionGroupName = value;
			_groupManager.addGroup(_unionGroupName);
		}
		
		public function set worldGroupName(value:String):void
		{
			_groupManager.delGroup(_worldGroupName);
			_worldGroupName = value;
			_groupManager.addGroup(_worldGroupName);
		}
		
		public function get sceneGroupName():String
		{
			return _sceneGroupName;
		}
		
		public function get teamGroupName():String
		{
			return _teamGroupName;
		}
		
		public function get battleGroupName():String
		{
			return _battleGroupName;
		}
		
		public function get unionGroupName():String
		{
			return _unionGroupName;
		}
		
		public function get worldGroupName():String
		{
			return _worldGroupName;
		}
		
		public function get worldGroup():DynamicNetGroup
		{
			return _groupManager.getGroup(_worldGroupName);
		}
		
		public function get unionGroup():DynamicNetGroup
		{
			return _groupManager.getGroup(_unionGroupName);
		}
		
		public function get battleGroup():DynamicNetGroup
		{
			return _groupManager.getGroup(_battleGroupName);
		}
		
		public function get teamGroup():DynamicNetGroup
		{
			return _groupManager.getGroup(_teamGroupName);
		}
		
		public function get sceneGroup():DynamicNetGroup
		{
			return _groupManager.getGroup(_sceneGroupName);
		}
		
		/******************************************************************************************************
		 * 
		 * 具体游戏逻辑
		 * udp消息分流
		 ******************************************************************************************************/
		
		public function onMessage(netGroup:DynamicNetGroup, info:Object):void{
			var msg:Array = info.message;
			var funcName:String = msg.shift();
			var func:Function = PacketHandler[funcName];
			if(func){
				func.apply(null, msg);
			}
		}
	}
}